package model

import (
	"errors"
	"stock/global"

	"gorm.io/gorm"
)

type LangModel struct {
	Base
}

type MallLang struct {
	Id           uint   `json:"id" gorm:"<-:false; primaryKey;column:id;type:int(11)"`
	TableName    string `json:"table_name" gorm:"<-:false; column:table_name;"`
	MerchantId   uint   `json:"merchant_id" gorm:"<-:false; column:merchant_id;"`
	MerchantType uint8  `json:"merchant_type" gorm:"<-:false; column:merchant_type;"`
	TableId      uint   `json:"table_id" gorm:"<-:false; column:table_id;"`
	Field        string `json:"field" gorm:"<-:false; column:field;"`
	Value        string `json:"value" gorm:"column:value;"`
	Lang         string `json:"lang" gorm:"<-:false; column:lang;"`
}

type RequestLang struct {
	TableName    string
	MerchantType uint8
	MerchantId   uint
	TableId      uint
	Field        []string
	Lang         string
}

type ResponseLang struct {
	MerchantName string `json:"merchant_name"`
	Address      string `json:"address"`
	MainProduct  string `json:"main_product"`
	Description  string `json:"description"`
	Describe     string `json:"describe"`
	Contactname  string `json:"contact_name"`
	ProductName  string `json:"product_name"`
	Name         string `json:"name"`
	Title        string `json:"title"`
	Detail       string `json:"detail"`
	// PurchaseDetail     string `json:"purchase_detail"`
	NewsArticleContent string `json:"news_article_content"`
	Content            string `json:"content"`
	Introduction       string `json:"introduction"`
}

func (m *LangModel) TabName() string {
	return global.CONF.Mysql.Prefix + "mall_lang"
}

func (m *LangModel) List(param RequestLang) (ResponseLang, error) {

	var reLang ResponseLang
	where := &MallLang{}
	if param.TableName != "" {
		where.TableName = param.TableName
	}
	if param.MerchantId != 0 {
		where.MerchantId = param.MerchantId
	}
	if param.MerchantType != 0 {
		where.MerchantType = param.MerchantType
	}
	if param.TableId != 0 {
		where.TableId = param.TableId
	}
	if param.Lang != "" {
		where.Lang = param.Lang
	}

	var lang []MallLang
	var tx *gorm.DB
	tx = global.DB.Debug().Where(where)
	if len(param.Field) > 0 {
		tx = tx.Where("field IN ?", param.Field)
	} else {
		return reLang, errors.New("field cannot be empty")
	}
	tx = tx.Order("id DESC").Find(&lang)
	if tx.Error != nil {
		return reLang, tx.Error
	}
	langLen := len(lang)
	var LangResponseArr ResponseLang
	if langLen > 0 {
		for i := 0; i < langLen; i++ {

			switch lang[i].Field {
			case "news_article_content":
				LangResponseArr.NewsArticleContent = lang[i].Value
				LangResponseArr.Content = lang[i].Value

			case "introduction":
				LangResponseArr.Introduction = lang[i].Value

			case "detail":
				LangResponseArr.Detail = lang[i].Value

			case "product_name":
				LangResponseArr.ProductName = lang[i].Value
				LangResponseArr.Name = lang[i].Value

			case "title":
				LangResponseArr.Title = lang[i].Value

			case "name":
				LangResponseArr.Name = lang[i].Value

			case "contact_name":
				LangResponseArr.Contactname = lang[i].Value

			case "describe":
				LangResponseArr.Describe = lang[i].Value

			case "description":
				LangResponseArr.Description = lang[i].Value

			case "main_product":
				LangResponseArr.MainProduct = lang[i].Value

			case "address":
				LangResponseArr.Address = lang[i].Value

			case "supplier_name":
				LangResponseArr.MerchantName = lang[i].Value

			case "purchase_name":
				LangResponseArr.MerchantName = lang[i].Value

			case "purchase_detail":
				LangResponseArr.Detail = lang[i].Value

			}
		}
	}
	return LangResponseArr, nil
}
